---
-- Misc funcs/vars you'll be wanting. Actually, it's just 4...
---

globals = {}
global_width, global_height = getMainWindowSize()

--Iterates through a table, looking for specified key
function table.keyFind(tbl, what)
   assert(type(tbl) == "table")
   
   for k,v in pairs(tbl) do
       if k == what then
         return true
       end
   end
   return false
end

---
-- Tabbed Chat init files 
-- Keep in mind screens can have different DPI settings, so you may need to change these
---


function chat_hideTab(channel)
	hideWindow(channel)
end

function chat_showTab(channel)
	showWindow(channel)
end

--Called whenever one of the 'tab' labels are clicked, just switches to that channel and highlights.
function chat_swapTab(channel)

	if globals["chatCurrent"] == channel then
		globals["chatCurrent"] = "none"
		chat_hideAll()
	else
		chat_hideTab(globals["chatCurrent"])
		local str = string.gsub(globals["chatCurrent"], "chat", "button")
		setBackgroundColor(str, i25,25,25, 0)
		chat_showTab(channel)
		str = string.gsub(channel, "chat", "button")
		setBackgroundColor(str, 50,50,50, 255)
		globals["chatCurrent"] = channel
	end
end


function chat_hideAll()
	--chat_hideTab("chatall")
	chat_hideTab("chattells")
	chat_hideTab("chatcity")
	chat_hideTab("chatguild")
	chat_hideTab("chatmarket")
	setBackgroundColor("buttonall", 0,0,0, 0)
	setBackgroundColor("buttontells", 0,0,0, 0)
	setBackgroundColor("buttoncity", 0,0,0, 0)
	setBackgroundColor("buttonguild", 0,0,0, 0)
	setBackgroundColor("buttonmarket", 0,0,0, 0)
end

function chatStartup()
_w, _h = getMainWindowSize();
_x, _y = calcFontSize( gui_chat_font )
global_chatHeight = _y * gui_chat_lines
global_chatWidth = _x * gui_chat_wrap

global_chatTabHeight = 15
global_chatTabWidth = 60

_x, _y = calcFontSize( gui_map_font )

global_chatPosX = _w - global_chatWidth 
global_chatPosY = 27 * _y
global_chatWindowWrap = gui_chat_wrap
	createLabel( "buttonall",0,0,0,0,0) 
	resizeWindow("buttonall", 
					global_chatTabWidth, 
					global_chatTabHeight)
	moveWindow("buttonall", 
				global_chatPosX, 
				global_chatPosY-global_chatTabHeight)
	setLabelClickCallback("buttonall", "chat_swapTab", "chatall")
	setMiniConsoleFontSize("buttonall", gui_chat_font )
	setTextFormat("buttonall", 25,25,25, 255,255,255, 0,0,0 )
	echo("buttonall", "<font color=\"grey\">&nbsp;&nbsp;&nbsp;All</font>")

	createLabel( "buttontells", global_chatPosX, global_chatPosY + global_chatTabHeight, global_chatTabWidth, global_chatTabHeight, 0)
	moveWindow( "buttontells", 
				global_chatPosX+global_chatTabWidth*1, 
				global_chatPosY-global_chatTabHeight )
	setLabelClickCallback("buttontells", "chat_swapTab", "chattells")
	setTextFormat("buttonall", 25,25,25, 255,255,255, 0,0,0 )
	echo("buttontells", "<font color=\"grey\">&nbsp;Tells</font>")

	createLabel( "buttoncity", global_chatPosX, global_chatPosY + 2 * global_chatTabHeight, global_chatTabWidth, global_chatTabHeight, 0)
	moveWindow( "buttoncity", 
				global_chatPosX+global_chatTabWidth*2, 
				global_chatPosY-global_chatTabHeight )	
	setLabelClickCallback("buttoncity", "chat_swapTab", "chatcity")
	setTextFormat("buttonall", 25,25,25, 255,255,255, 0,0,0 )
	echo("buttoncity", "<font color=\"grey\">&nbsp;&nbsp;City</font>")

	createLabel( "buttonguild", global_chatPosX, global_chatPosY + 3 * global_chatTabHeight, global_chatTabWidth, global_chatTabHeight, 0)
	moveWindow( "buttonguild", 
				global_chatPosX+global_chatTabWidth*3, 
				global_chatPosY-global_chatTabHeight )
	setLabelClickCallback("buttonguild", "chat_swapTab", "chatguild")
	setTextFormat("buttonall", 25,25,25, 255,255,255, 0,0,0 )
	echo("buttonguild", "<font color=\"grey\">&nbsp;Guild</font>")

	createLabel( "buttonmarket", global_chatPosX, global_chatPosY + 4 * global_chatTabHeight, global_chatTabWidth, global_chatTabHeight, 0)
	moveWindow( "buttonmarket", 
				global_chatPosX+global_chatTabWidth*4, 
				global_chatPosY-global_chatTabHeight )
	setLabelClickCallback("buttonmarket", "chat_swapTab", "chatmarket")
	setTextFormat("buttonall", 25,25,25, 255,255,255, 0,0,0 )
	echo("buttonmarket", "<font color=\"grey\">&nbsp;Market</font>")


	--"All" Tab
	createMiniConsole("chatall", 0, 0, 0, 0)
	setWindowWrap("chatall", global_chatWindowWrap)
	setMiniConsoleFontSize("chatall", gui_chat_font );
	setBackgroundColor("chatall", 0, 0, 0, 0)
	resizeWindow("chatall", global_chatWidth, global_chatHeight );
	moveWindow("chatall", global_chatPosX, global_chatPosY)
	--chat_hideTab("chatall")

	--"Tells" Tab
	createMiniConsole("chattells", 0, 0, 0, 0)
	setWindowWrap("chattells", global_chatWindowWrap)
	setMiniConsoleFontSize("chattells", gui_chat_font );
	setBackgroundColor("chattells", 0, 0, 0, 0)
	resizeWindow("chattells", global_chatWidth, global_chatHeight );
	resizeWindow("chattells", global_chatWidth, global_chatHeight );
	moveWindow("chattells", global_chatPosX, global_chatPosY)
	chat_hideTab("chattells")

	--"City" Tab
	createMiniConsole("chatcity", 0, 0, 0, 0)
	setWindowWrap("chatcity", global_chatWindowWrap)
	setMiniConsoleFontSize("chatcity", gui_chat_font );
	setBackgroundColor("chatcity", 0, 0, 0, 0)
	resizeWindow("chatcity", global_chatWidth, global_chatHeight );
	moveWindow("chatcity", global_chatPosX, global_chatPosY)
	chat_hideTab("chatcity")

	--"Guild/House" Tab
	createMiniConsole("chatguild", global_chatPosX + global_chatTabWidth,global_chatPosY, global_chatWidth, global_chatHeight)
	setWindowWrap("chatguild", global_chatWindowWrap)
	setMiniConsoleFontSize("chatguild", gui_chat_font );
	setBackgroundColor("chatguild", 0, 0, 0, 0)
	resizeWindow("chatguild",global_chatWidth, global_chatHeight );
	moveWindow("chatguild", global_chatPosX, global_chatPosY)
	chat_hideTab("chatguild")

	--"market" Tab
	createMiniConsole("chatmarket", global_chatPosX + global_chatTabWidth,global_chatPosY, global_chatWidth, global_chatHeight)
	setWindowWrap("chatmarket", global_chatWindowWrap)
	setMiniConsoleFontSize("chatmarket", gui_chat_font );
	setBackgroundColor("chatmarket", 0, 0, 0, 0)
	resizeWindow("chatmarket",global_chatWidth, global_chatHeight );
	moveWindow("chatmarket", global_chatPosX, global_chatPosY)
	chat_hideTab("chatmarket")


	--Show the current tab, which defaults to 'All', if the global doesn't exist:
	if table.keyFind(globals, "chatCurrent") == false then
	  globals["chatCurrent"] = "none"
	end
	--chat_showTab(globals["chatCurrent"])
	--Highlight the tab
	--local str = string.gsub(globals["chatCurrent"], "chat", "button")
	--setBackgroundColor(str, 25,25,25, 255)
end
